package com.example.electronicmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.electronicmall.R;
import com.example.electronicmall.bean.NewsMassageBean;

import java.util.List;

/*
 * 时间：2022/11/1
 * 作者：王辉
 * 功能：消息列表适配器
 * */
public class NewsMessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
List<NewsMassageBean> newsMassageBeanList;
    private OnClickExperintListener listener;
    public NewsMessageListAdapter(Context context, List<NewsMassageBean> newsMassageBeanList) {
        this.context = context;
        this.newsMassageBeanList = newsMassageBeanList;

    }
    public void setListener(OnClickExperintListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        return new NewsMessageListHolder(LayoutInflater.from(context).inflate(R.layout.news_newslist,null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((NewsMessageListHolder)holder).messageicon.setImageResource(Integer.parseInt(newsMassageBeanList.get(position).getNewsicon()));
        ((NewsMessageListHolder)holder).messagetitle.setText(newsMassageBeanList.get(position).getNewstitle());
        ((NewsMessageListHolder)holder).messagecontent.setText(newsMassageBeanList.get(position).getNewscontext());
        ((NewsMessageListHolder)holder).messagetime.setText(newsMassageBeanList.get(position).getNewstime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClickExperint(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsMassageBeanList==null?0:newsMassageBeanList.size();
    }

    class NewsMessageListHolder extends RecyclerView.ViewHolder{
         ImageView messageicon;
         TextView  messagetitle;
         TextView  messagecontent;
         TextView  messagetime;
        public NewsMessageListHolder(@NonNull View itemView) {
            super(itemView);
            messageicon=itemView.findViewById(R.id.news_newslist_icon);
            messagetitle=itemView.findViewById(R.id.news_newslist_title);
            messagecontent=itemView.findViewById(R.id.news_newslist_content);
            messagetime=itemView.findViewById(R.id.news_newslist_time);
        }
    }
    public interface OnClickExperintListener{
        void onClickExperint(int position);
    }
}
